package com.drow.aips;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.drow.aips.util.DialogUtil;
import com.drow.aips.util.HttpUtil;

/**
 * 
 * 类名称：Record.java
 * <P/>
 * 类描述： 通知记录查看类
 * <P/>
 * 创建时间：2012-12-24
 * <P/>
 * 创建人： 史守阳
 * <P/>
 * 联系方式： ssy.vip^qq.com
 * <P/>
 * 修改人：无
 * <P/>
 * 修改时间：无
 * <P/>
 * 修改备注：无
 * <P/>
 * 版本：v1.0
 *
 */
public class Record extends ListActivity {

	ListView listView = null;
	MyListAdapter myAdapter = null;
	
	ArrayList<String> titleList  = null;
	ArrayList<String> typeList = null;
	ArrayList<String> infoList = null;
	ArrayList<String> timeList = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.record);
		
		listView = this.getListView();
		
		 //初始化titleList,typeList,infoList,timeList数据
    	initRecord();
		
		myAdapter = new MyListAdapter(titleList,typeList,infoList,timeList);
		listView.setAdapter(myAdapter);
	}


	private void initRecord() {
		
		titleList = new ArrayList<String>();
		typeList = new ArrayList<String>();
		infoList = new ArrayList<String>();
		timeList = new ArrayList<String>();
		
		// 定义发送请求的URL
    	String url = HttpUtil.BASE_URL + "record.jsp";
    	
		try
		{
			// 发送请求
	    	JSONObject jsonObj = new JSONObject(HttpUtil.getRequest(url));
	    	
	    	JSONArray titleArrayTemp = jsonObj.getJSONArray("titleArray");
	    	
	    	JSONArray typeArrayTemp = jsonObj.getJSONArray("typeArray");
	    	
	    	JSONArray infoArrayTemp = jsonObj.getJSONArray("infoArray");
	    	
	    	JSONArray timeArrayTemp = jsonObj.getJSONArray("timeArray");
	    	
	    	for(int i=0,length=titleArrayTemp.length();i<length;i++)
	    	{
	    		titleList.add(titleArrayTemp.getString(i));
	    		typeList.add(typeArrayTemp.getString(i));
	    		infoList.add(infoArrayTemp.getString(i));
	    		timeList.add(timeArrayTemp.getString(i));
	    	}
	    	
		}
		catch (Exception e)
		{
			DialogUtil.showDialog(this, "服务器响应异常，请稍后再试！", false);
			e.printStackTrace();
		}
		
	}


	class MyListAdapter extends BaseAdapter {
		View[] itemViews;  
		  
        public MyListAdapter(ArrayList<String> titleList, ArrayList<String> typeList ,ArrayList<String> infoList ,ArrayList<String> timeList) {  
            itemViews = new View[titleList.size()];  
  
            for (int i = 0; i < itemViews.length; i++) {  
                itemViews[i] = makeItemView(titleList.get(i), typeList.get(i), infoList.get(i), timeList.get(i));  
            }  
        }  
  
        public int getCount() {  
            return itemViews.length;  
        }  
  
        public View getItem(int position) {  
            return itemViews[position];  
        }  
  
        public long getItemId(int position) {  
            return position;  
        }  
  
        private View makeItemView(String titleString, String typeString ,String infoString ,String timeString) {  
        	
            LayoutInflater inflater = (LayoutInflater) Record.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  
            // 使用View的对象itemView与R.layout.item关联   
            View itemView = inflater.inflate(R.layout.record_item, null); 
            
            // 通过findViewById()方法实例R.layout.item内各组件   
            TextView title = (TextView) itemView.findViewById(R.id.record_title);  
            title.setText(titleString);  
            TextView type = (TextView) itemView.findViewById(R.id.record_type);  
            type.setText(typeString);  
            TextView info = (TextView) itemView.findViewById(R.id.record_info);  
            info.setText(infoString);  
            TextView time = (TextView) itemView.findViewById(R.id.record_time);  
            time.setText(timeString); 
              
            return itemView;  
        }

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
				View itemView = null;
				
				if (convertView == null)
				{
					itemView = itemViews[position];
				}else
				{
					itemView = convertView;
					
					// 通过findViewById()方法实例R.layout.item内各组件   
		            TextView title = (TextView) itemView.findViewById(R.id.record_title);  
		            title.setText(titleList.get(position));  
		            
		            TextView type = (TextView) itemView.findViewById(R.id.record_type);  
		            type.setText(typeList.get(position));  
		            
		            TextView info = (TextView) itemView.findViewById(R.id.record_info);  
		            info.setText(infoList.get(position));  
		            
		            TextView time = (TextView) itemView.findViewById(R.id.record_time);  
		            time.setText(timeList.get(position));  
				}
				
	            return itemView;  
			}
		}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有", Toast.LENGTH_LONG).show();
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
		{
			// 启动Main Activity
			Intent intent = new Intent(Record.this, Home.class);
			startActivity(intent);
			// 结束该Activity
			finish();
		}else if(keyCode == KeyEvent.KEYCODE_MENU) 
		{
			Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有", Toast.LENGTH_LONG).show();
		}
		return true;
	}
}